package com.mhys.hms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.entity.User;
import com.mhys.hms.service.user.UserInfo;
import com.mhys.hms.service.user.UserService;

/**
 *  登陆controller
 * @author 
 *
 */
@Controller
public class Login {
	@Autowired
	private UserInfo userInfo;
	@Autowired
	private UserService userService;
	
	
	/**
	 *  登陆验证
	 * @param userName 用户名
	 * @param pwd 密码
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public FeedBackObject login(HttpSession session ,String userName,String pwd) {
		FeedBackObject fbo = new FeedBackObject();
		List<User> list = userInfo.getUser(userName,pwd);
		if(list.size()>0) {
			session.setAttribute("user", userName);
			fbo.msg = "OK";
//			System.out.println(list.get(0).getName());
			fbo.Obj = list;
//			System.out.println("222");
		}else {
			fbo.success = "FAIL";
//			System.out.println("111111");
		}
		return fbo;
	}
	
	/**
	 *   跳转到主页面
	 * @return
	 */
	@RequestMapping("/main")
	public String toMainPage(HttpSession session) {
		//判断用户是否登陆
		String userName = (String) session.getAttribute("user");
		if(userName!=null) {
			//通过视图解析器转发
			return "WEB-INF/page/mainPage";
			
		}
		return "err";
	}
	
	@RequestMapping("/readUserCount")
	@ResponseBody
	public FeedBackObject readUserCount(HttpSession session) {
		FeedBackObject fbo = new FeedBackObject();
//		獲取用戶名
		String userName = (String) session.getAttribute("user");
		List<User> list = userService.queryUserByCount(userName);
		fbo.Obj = list.get(0);
		fbo.msg = "OK";
		return fbo;
	}
}
